package com.ikun.appointment.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikun.appointment.dao.ConventionDao;
import com.ikun.appointment.dao.PatientDao;
import com.ikun.common.client.DockerServiceClient;
import com.ikun.common.client.SchedulingServiceClient;
import com.ikun.common.entity.Convention;
import com.ikun.appointment.service.ConventionService;
import com.ikun.common.entity.Docker;
import com.ikun.common.entity.PageEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


/**
 * @author sun12
 */
@Service("conventionService")
public class ConventionServiceImpl extends ServiceImpl<ConventionDao, Convention> implements ConventionService {

    @Resource
    private PatientDao patientDao;
    @Resource
    private ConventionDao conventionDao;
    @Resource
    private DockerServiceClient dockerServiceClient;
    @Resource
    private SchedulingServiceClient schedulingServiceClient;


    @Override
    public Page<Convention> findPage(Long current, Long size, Integer userId, Integer state) throws NoSuchFieldException {
        Page<Convention> page = new Page<>(current, size);
        Page<Convention> conventionPage = conventionDao.selectPage(page, userId, state);

        HashMap<String, String> map = new HashMap<>();
        Map<String, String> search = new HashMap<>();
        for (Convention record : conventionPage.getRecords()) {
            //给预约信息添加患者信息
            record.setPatient(patientDao.selectById(record.getPatId()));
            //给预约信息添加医生信息
            //根据id查询出医生信息
            search.put("id", record.getDocId().toString());
            map.put("search", JSON.toJSONString(search));
            ResponseEntity<PageEntity<Docker>> entity = dockerServiceClient.searchDocker(map);
            //将查询出来的医生的信息赋值给挂号信息
            record.setDocker(Objects.requireNonNull(entity.getBody()).getData().get(0));
            //record.setDocker(entity.getBody().getData().get(0));
            //给预约信息添加科室信息
            record.setDepartment(entity.getBody().getData().get(0).getDepartment());
            //给预约信息添加挂号类型信息
            record.setRegistrationType(schedulingServiceClient.findTypeById(record.getTypeId()).getBody());
            //给预约信息添加预约时间段信息
            record.setTimePeriod(schedulingServiceClient.findTimeById(record.getTimeId()).getBody());
        }
        return conventionPage;
    }

    @Override
    public void deleteById(Integer id) {
        try {
            //根据id查询出预约信息，并修改删除状态
            Convention convention = conventionDao.selectById(id);
            convention.setDel(1);
            conventionDao.updateById(convention);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Integer findCount(Integer userId, Integer state) {
        return conventionDao.selectCount(userId, state);
    }

}

